<?php
namespace view;
require_once('time.php');
require_once('cookieEater.php');

class PageView{
	
	private static $username = "username";
	private static $password = "password";
	private static $logOutButton = 'logOutButton';
	private static $logInButton = 'logInButton';
	private static $cookieUsername = 'cookieUsername';
	private static $cookiePassword = 'cookiePassword';
	private static $checkbox = 'checkbox';
	private static $salt = "salt";
	private static $cryptPassword = 'Password';
	private static $message;
	
	public function getUsername(){
		if(isset($_POST[self::$username])){
			return $_POST[self::$username];
		}
	}
	public function getPassword(){
		if(isset($_POST[self::$password])){
			return $_POST[self::$password];
		}
	}
	public function getlogOutButton(){
		if(isset($_POST[self::$logOutButton])){
			return TRUE;
		}
		return FALSE;
	}
	public function getlogInButton(){
		if(isset($_POST[self::$logInButton])){
			return TRUE;;
		}
		return FALSE;
	}
	public function getcheckbox(){
		if(isset($_POST[self::$checkbox])){
			return TRUE;;
		}
		return FALSE;
	}
	public function getcookieUsername(){
		if(isset($_COOKIE[self::$username])){
			return $_COOKIE[self::$username];
		}
		return FALSE;
	}
	public function getcookiePassword(){
		if(isset($_COOKIE[self::$password])){
			return $_COOKIE[self::$password];
		}
		return FALSE;
	}
	
	/* checks if logged in
	 * @return html
	 */
	public function checkIfLoggedIn($messageNr, $sessionLoggedIn){	
		
		$this->setMessage($messageNr);
		
		if($sessionLoggedIn == TRUE){
			$html = $this->loggedInPage();
		}else{
			$html = $this->printLoginForm();
		}
		return $html;
	}
	
	/*
	 * sets self::$message
	 */
	private function setMessage($messageNr){
		switch($messageNr){
			case 0: self::$message = "" ;
			break;
			case 1: self::$message = 'Inloggning lyckades';
			break;
			case 2: self::$message = 'Inloggning lyckades och dina uppgifter är sparade';
			break;
			case 3: self::$message = 'Användarnamn saknas';
			break;
			case 4: self::$message = 'Lösenord saknas';
			break;
			case 5: self::$message = 'Felaktigt användarnamn och/eller lösenord';
			break;
			case 6: self::$message = 'Du har loggats ut';
			break;
			case 7: self::$message = "Sessions Tjuv!";
			break;
			case 8: self::$message = "Cookie to old!";
			break;
			case 9: self::$message = 'Inloggning lyckades med cookies nomnomnom';
			break;
			case 10: self::$message = "Fel information i cookies ";
			break;
			default: self::$message = "Message e inte satt!";
			break;
		}
	}
	
	//@return html
	private function printLoginForm() {
		
		$this->destroyCookie();
	 	$html = '<!DOCTYPE html>
	 		<head>
	 		<meta charset="utf-8">
	 		<title>Benny är Awesome!</title>
	 		</head>
	 		<body>
	 		<h1>Ej Inloggad!</h1>
	 		';
			$html .= self::$message;
		$usernameValue = null;
		if (isset($_POST[self::$username])) {
			$usernameValue = $_POST[self::$username];	
		}
	 	$html .= 
	 		"<form id='logInForm' action='index.php?attempt' method='post'>
	 		Användarnamn: 
	 		<input type='text' Name='username' id='username' value='$usernameValue' />";		
	 	$html .=	
	 		'Lösenord: 
	 		<input type="password" Name="password" id="password" />
	 		<input type="checkbox" name="checkbox" value="1" id="checkbox"/>Remember Me!
	 		<button type="submit" name="logInButton">Log In</button>
	 		</form>
	 		'. $this->theTime() .'
	 		</body>
	 		</html>';
	 	return $html;
	}
	
	//@return html
	private function loggedInPage(){
			
		$this->checkCheckbox();
		
 		$html = '<!DOCTYPE html>
 		<meta charset="utf-8">
    	<body>';
		$html .= self::$message;
	 	$html .='	
	 		<h1>Admin är inloggad!</h1>
	 		<form action="index.php" method="post">
	 			<button type="buttton" name="logOutButton" 
	 			id="logOutButton">Log Out
	 			</button>
			</form>
			'. $this->theTime() .  $this->eatCookies() .'
	 		</body>
	 		</html>
	 		';
	 
	 	return $html;
	}
	
	/*checks if remember me box is set
	 *calls method to set cookies
	 * changes the message
	 */ 
	private function checkCheckbox(){
		if(isset($_POST[self::$checkbox])){
			$this->setCookies();
			$messageNr = 2;
			$this->setMessage($messageNr);
		}
	}

	private function setCookies(){
		$cookieTimer = time() + 60;
		file_put_contents("endtime.txt", "$cookieTimer");
		setcookie(self::$username, $_POST[self::$username] ,$cookieTimer);
		self::$cryptPassword = md5($_POST[self::$password] . self::$salt);
		setcookie(self::$password, self::$cryptPassword ,$cookieTimer);
	}

	/*
	 * destroys cookies
	 */
	private function destroyCookie(){
		setcookie(self::$username,'',time()-3600);
		setcookie(self::$username,'',time()-3600);
	}

	//gets the time
	private function theTime(){
	
		$time = new time();
		return $time->theTime();
	}
	
	//eat cookies
	private function eatCookies(){
		$cookieEater = new cookieEater();		
		return $cookieEater->eatCookies();
	}
}
